#!/bin/bash

# Checks before Upgrade to 8.0
/usr/bin/mysqlcheck -pxinetrlz -s --all-databases --check-upgrade
if [ $? -ne 0 ]; then
	echo "mysqlcheck failed before upgrade from 5.7 to 8.0, stopping..."
	echo "Please run mysqlcheck -g -s -A and fix the errors that popup manually.."
	exit 1
fi

# Run with MySQL 5.7
/usr/bin/mysql -pxinetrlz --execute="XA RECOVER;"
/usr/bin/mysql -pxinetrlz --execute="SET GLOBAL innodb_fast_shutdown=0;"

echo "shutdown MySQL 5.7"
/usr/etc/venture/bin/dbmgr -shutdown
sleep 30
/usr/bin/mysqladmin -uroot -pxinetrlz shutdown
sleep 30

# Update my.cnf
mycnf=/usr/etc/mysql/var/my.cnf
MY_PRINT_DEFAULTS_DIR=/usr/etc/venture/bin
if [ $(grep -c set-variable $mycnf) -gt 0 -o $(grep -c record_buffer $mycnf) -gt 0 -o $($MY_PRINT_DEFAULTS_DIR/my_print_defaults mysqld | grep -c default-character-set) -gt 0 ]; then
	/bin/rm -f "$mycnf".old
	cp -p $mycnf "$mycnf".old
	cmp -s $mycnf "$mycnf".old
	if [ $? -ne 0 ]; then
		echo "Could not save $mycnf to $mycnf.old"
		echo ABORTING Venture UPGRADE ...
		exit 1
	fi
	sed -e "s/set-variable[	 ]*=[ 	]*//" -e /skip-locking/d -e "/table_cache[ 	]*=/d" \
		-e "/record_buffer[ 	]*=/d" -e /default-character-set/d \
		-e /thread_concurrency/d -e "s/sort_buffer[ 	]*=/sort_buffer_size=/" \
		-e "s/thread_cache[ 	]*=/thread_cache_size=/" <"$mycnf".old >$mycnf
# XXX the client default-character-set is now gone, but "dbmgr -checkdbcharset" puts it back
fi

# Remove MySQL 8.0 removed options from my.cnf
if [ $(grep -c innodb_large_prefix $mycnf) -gt 0 -o $(grep -c innodb_file_format $mycnf) -gt 0 -o $(grep -c query_cache_size $mycnf) -gt 0 -o $(grep -c query_cache_size $mycnf) -gt 0 -o $(grep -c query_cache_limit $mycnf) -gt 0 -o $(grep -c query_cache_type $mycnf) -gt 0 ]; then
	/bin/rm -f "$mycnf".old
	cp -p $mycnf "$mycnf".old
	cmp -s $mycnf "$mycnf".old
	if [ $? -ne 0 ]; then
		echo "Could not save $mycnf to $mycnf.old"
		echo ABORTING Venture UPGRADE ...
		exit 1
	fi
	sed -e "/innodb_large_prefix[ 	]*=/d" -e "/innodb_file_format[ 	]*=/d" \
		-e "/query_cache_size[ 	]*=/d" -e "/query_cache_size[ 	]*=/d" \
		-e "/query_cache_limit[ 	]*=/d" -e "/query_cache_type[ 	]*=/d" <"$mycnf".old >$mycnf
fi

if [ $(grep -c key_buffer $mycnf) -gt 0 -o $(grep -c sort_buffer $mycnf) -gt 0 -o $(grep -c read_buffer $mycnf) -gt 0 -o $(grep -c write_buffer $mycnf) -gt 0 ]; then
	/bin/rm -f "$mycnf".old
	cp -p $mycnf "$mycnf".old
	cmp -s $mycnf "$mycnf".old
	if [ $? -ne 0 ]; then
		echo "Could not save $mycnf to $mycnf.old"
		echo ABORTING Venture UPGRADE ...
		exit 1
	fi
	sed -e "/key_buffer[ 	]*=/d" -e "/sort_buffer[ 	]*=/d" \
		-e "/read_buffer[ 	]*=/d" -e "/write_buffer[ 	]*=/d" <"$mycnf".old >$mycnf
fi

if grep binary_as_hex $mycnf >/dev/null 2>&1; then
	sed -i 's/binary_as_hex.*$/binary_as_hex=0/' $mycnf
else
	sed -i 's/\[client\]/[client]\binary_as_hex=0/' $mycnf
fi

# Download MySQL 8.0 package
echo "Removing Libs dependency for MySQL 8.0 upgrade (to avoid package name conflicts)."
yum -y autoremove mysql-community-libs
yum -y autoremove mysql-community-release
yum -y autoremove mysql57-community-release-el7-9.noarch
yum -y autoremove mysql80-community-release-el7-3.noarch

echo "Installing MySQL 8.0..."
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022 || exit 1
wget http://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm || exit 1
rpm -ivh mysql80-community-release-el7-3.noarch.rpm || exit 1
yum -y install mysql-server mysql-devel || exit 1
rm -f mysql80-community-release-el7-3.noarch.rpm

echo "Removing /etc/my.cnf"
/bin/rm -f /etc/my.cnf
echo "Linking /etc/my.cnf to $mycnf"
ln -s $mycnf /etc/my.cnf

if grep innodb_page_size $mycnf >/dev/null 2>&1; then
	sed -i 's/innodb_page_size.*$/innodb_page_size=32K/' $mycnf
else
	sed -i 's/\[mysqld\]/[mysqld]\ninnodb_page_size=32K/' $mycnf
fi

echo "Start MySQL 8.0..."
/usr/sbin/mysqld -uroot --datadir=/usr/etc/mysql/data --sql-mode=NO_ENGINE_SUBSTITUTION &
sleep 120

echo "Repairing file table.."
/usr/bin/mysql -pxinetrlz --execute="REPAIR TABLE webnative.file USE_FRM"

echo "Fixing AnnotationsComments Table"
# In https://github.com/trilogy-group/eng-maintenance/issues/3850 we learnt that some backups may contain duplicate entries for Comment ID.
# Therefore, it can't be a PK.
# Since it's obvious the purpose of this table is enabling an N-N relationship between Annotations and Comments, we didn't need it to be the PK either.
# Also, because of the additional restrictions of InnoDB, we can't use a composite PK (CommentID,AnnotationID),
# because we always hit an "ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key" when running
# the last command of preinstall-step8
# Considering the table's purpose, it's safe to define a new column to serve as the PK. CommentID doesn't need to be AUTO-INCREMENT, in fact it doesn't make sense - it's just a FK
# We keep 2 indices as before, but instead of using only CommentID fro one of them, we use a composite one (because of the duplicated entries).
/usr/bin/mysql -pxinetrlz webnative -e "START TRANSACTION;"
/usr/bin/mysql -pxinetrlz webnative -e "ALTER TABLE annotationscomments MODIFY CommentID int unsigned NOT NULL;"
/usr/bin/mysql -pxinetrlz webnative -e "ALTER TABLE annotationscomments DROP PRIMARY KEY;"
/usr/bin/mysql -pxinetrlz webnative -e "ALTER TABLE annotationscomments ADD ID int unsigned NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY(ID);"
/usr/bin/mysql -pxinetrlz webnative -e "CREATE UNIQUE INDEX by_comment_id_annotation_id ON annotationscomments (CommentID,AnnotationID);"
/usr/bin/mysql -pxinetrlz webnative -e "CREATE INDEX by_annotation_id ON annotationscomments (AnnotationID);"
/usr/bin/mysql -pxinetrlz webnative -e "COMMIT;"

echo "Fixing Event Table"
MAXAUTO=$(/usr/bin/mysql -pxinetrlz "--binary-as-hex=0" webnative -sse "SELECT IFNULL(MAX(EventID),1) FROM event;")
/usr/bin/mysql -pxinetrlz webnative -e "START TRANSACTION;"
/usr/bin/mysql -pxinetrlz webnative -e "ALTER TABLE event AUTO_INCREMENT = $MAXAUTO, DROP PRIMARY KEY, ADD PRIMARY KEY(EventID,FileID);"
/usr/bin/mysql -pxinetrlz webnative -e "CREATE INDEX BY_FILEID ON event (FileID);"
/usr/bin/mysql -pxinetrlz webnative -e "COMMIT;"

echo "Fixing AnnotationsRect Table"
/usr/bin/mysql -pxinetrlz webnative -e "START TRANSACTION;"
/usr/bin/mysql -pxinetrlz webnative -e "CREATE UNIQUE INDEX by_annotation_id ON annotationsrect (AnnotationID);"
/usr/bin/mysql -pxinetrlz webnative -e "COMMIT;"
